// pages/vip/vip.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    points: -1,
    couponList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    wx.request({
      url: 'http://localhost:8081/api/coupon/wxGetCouponList',
      method: 'GET',
      success: function (res) {
        if (res.data.code == 10008) {
          // that.setData({
          //   couponList : res.data.result
          // })
          for(var y =0;y<res.data.result.length;y++){
            var tar = res.data.result[y].couponEndTime
            var tarY = res.data.result[y].couponEndTime.substring(0,4)
            var tarM = res.data.result[y].couponEndTime.substring(5,7)
            var tarD = res.data.result[y].couponEndTime.substring(8,10)
            var tarHHmm = res.data.result[y].couponEndTime.substring(11,16)
            var target = tarY + '年' + tarM + '月' + tarD + '日' + ' ' + tarHHmm
            res.data.result[y].couponEndTime = target
          }
          that.setData({
            couponList : res.data.result
          })
          that.setData({
            couponList: res.data.result
          })
        } else {
          wx.showToast({
            title: '数据加载中',
            icon: 'loading'
          })
        }
      }
    })
    if (wx.getStorageSync('userInfo') == '') {
      return
    }
    wx.request({
      url: 'http://localhost:8081/api/balance/getBalanceByUserId',
      method: 'GET',
      data: {
        userId: wx.getStorageSync('userInfo').id
      },
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        that.setData({
          points: res.data.result.points
        })
      }
    })
  },

  // 兑换
  conversion(e) {
    var that = this
    if (wx.getStorageSync('userInfo') != '') {
      wx.showModal({
        title: '提示',
        content: '是否兑换？',
        success(res) {
          if (res.confirm) {
            wx.request({
              url: 'http://localhost:8081/api/coupon/conversionCoupon',
              data: {
                couponId: e.currentTarget.dataset.tar.id,
                userId: wx.getStorageSync('userInfo').id
              },
              method: 'GET',
              header: {
                'token': wx.getStorageSync('token')
              },
              success: function (res) {
                if (res.data.result == '兑换成功') {
                  wx.showToast({
                    title: res.data.result,
                  })
                  that.onShow()
                } else {
                  wx.showToast({
                    title: '积分不足',
                    icon: 'error'
                  })
                }
              }
            })
          }
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    if (wx.getStorageSync('userInfo') == '') {
      return
    }
    wx.request({
      url: 'http://localhost:8081/api/balance/getBalanceByUserId',
      method: 'GET',
      data: {
        userId: wx.getStorageSync('userInfo').id
      },
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        that.setData({
          points: res.data.result.points
        })
      }
    })
    wx.request({
      url: 'http://localhost:8081/api/coupon/wxGetCouponList',
      method: 'GET',
      success: function (res) {
        if (res.data.code == 10008) {
          // that.setData({
          //   couponList : res.data.result
          // })
          for(var y =0;y<res.data.result.length;y++){
            var tar = res.data.result[y].couponEndTime
            var tarY = res.data.result[y].couponEndTime.substring(0,4)
            var tarM = res.data.result[y].couponEndTime.substring(5,7)
            var tarD = res.data.result[y].couponEndTime.substring(8,10)
            var tarHHmm = res.data.result[y].couponEndTime.substring(11,16)
            var target = tarY + '年' + tarM + '月' + tarD + '日' + ' ' + tarHHmm
            res.data.result[y].couponEndTime = target
          }
          that.setData({
            couponList : res.data.result
          })
          that.setData({
            couponList: res.data.result
          })
        } else {
          wx.showToast({
            title: '数据加载中',
            icon: 'loading'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})